#include <string.h>
#include "model/types.h"
#include "utils/computer.h"
#include <gtk/gtk.h>

GtkWidget *num1_entry, *num2_entry, *result_label;

void add_button_clicked(GtkWidget *widget, gpointer data);

void mul_button_clicked(GtkWidget *widget, gpointer data);

int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);

    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "大整数计算器");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    GtkWidget *label = gtk_label_new("吴竹亚");
    gtk_container_add(GTK_CONTAINER(vbox), label);

    GtkWidget *hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_container_add(GTK_CONTAINER(vbox), hbox1);
    GtkWidget *num1_label = gtk_label_new("第一个数:");
    gtk_container_add(GTK_CONTAINER(hbox1), num1_label);
    num1_entry = gtk_entry_new();
    gtk_container_add(GTK_CONTAINER(hbox1), num1_entry);

    GtkWidget *hbox2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_container_add(GTK_CONTAINER(vbox), hbox2);
    GtkWidget *num2_label = gtk_label_new("第二个数:");
    gtk_container_add(GTK_CONTAINER(hbox2), num2_label);
    num2_entry = gtk_entry_new();
    gtk_container_add(GTK_CONTAINER(hbox2), num2_entry);

    GtkWidget *hbox3 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_container_add(GTK_CONTAINER(vbox), hbox3);
    GtkWidget *add_button = gtk_button_new_with_label("加");
    g_signal_connect(add_button, "clicked", G_CALLBACK(add_button_clicked), NULL);
    gtk_container_add(GTK_CONTAINER(hbox3), add_button);
    GtkWidget *mul_button = gtk_button_new_with_label("乘");
    g_signal_connect(mul_button, "clicked", G_CALLBACK(mul_button_clicked), NULL);
    gtk_container_add(GTK_CONTAINER(hbox3), mul_button);

    GtkWidget *hbox4 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_container_add(GTK_CONTAINER(vbox), hbox4);
    result_label = gtk_label_new("");
    gtk_container_add(GTK_CONTAINER(hbox4), result_label);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

void add_button_clicked(GtkWidget *widget, gpointer data) {
    const char *num1 = gtk_entry_get_text(GTK_ENTRY(num1_entry));
    const char *num2 = gtk_entry_get_text(GTK_ENTRY(num2_entry));
    NODE *head1, *head2;
    initLink(&head1);
    initLink(&head2);
    repToLink(head1, num1);
    repToLink(head2, num2);
    NODE *head[] = {head1, head2};
    // 进行大数相加的相关操作
//            add(head1, head2, &result);
    addBig(head, 0, 2);
    char resultStr[1024];
    memset(resultStr, 0, 1024);
    getStr(*head[1], resultStr);
    char temp[1024];
    memset(temp, 0, 1024);
    strcpy(temp,num1);
    strcat(temp, "+");
    strcat(temp, num2);
    strcat(temp, "=");
    strcat(temp, resultStr);
    gtk_label_set_text(result_label, temp);
}

void mul_button_clicked(GtkWidget *widget, gpointer data) {
    const char *num1 = gtk_entry_get_text(GTK_ENTRY(num1_entry));
    const char *num2 = gtk_entry_get_text(GTK_ENTRY(num2_entry));
    NODE *head1, *head2, *result;
    initLink(&head1);
    initLink(&head2);
    repToLink(head1, num1);
    repToLink(head2, num2);

    // 进行大数相加的相关操作
    mulBig(head1, head2, &result);
    char resultStr[1024];
    memset(resultStr, 0, 1024);
    getStr(*result, resultStr);
    char temp[1024];
    memset(temp, 0, 1024);
    strcpy(temp,num1);
    strcat(temp, "*");
    strcat(temp, num2);
    strcat(temp, "=");
    strcat(temp, resultStr);
    gtk_label_set_text(result_label, temp);
}